package com.luocj.elephant.demo.view;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PaintFlagsDrawFilter;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

import com.luocj.elephant.demo.R;

/**
 * Created by luocj515 on 2017/10/25.
 * 项目名称：CustomViewActivity
 * 类描述：
 * 创建人：hasee
 * 创建时间：2017/10/25 11:18
 * 修改备注：
 */

public class NewRingView extends View {

    //中间进度颜色
    private static final int GREEN_COLOR = 0xFF06C494;

    private static final int GRAY_COLOR = 0xFFBEBBB4;

    // 宽度
    private int width;

    // 高度
    private int height;

    // 半径
    private int radius;

    // 指针图片
    private Bitmap mBitmap;

    // 指针图片宽度
    private int mBitmapWidth;

    // 指针图片高度
    private int mBitmapHeight;

    // 最外层渐变圆环画笔
    private Paint mGradientRingPaint;

    // 大刻度画笔
    private Paint mSmallCalibrationPaint;

    // 小刻度画笔
    private Paint mBigCalibrationPaint;

    // 中间进度圆环画笔
    private Paint mMiddleRingPaint;

    // 内虚线圆环画笔
    private Paint mInnerRingPaint;

    // 外层圆环文本画笔
    private Paint mTextPaint;

    // 中间进度圆环画笔
    private Paint mMiddleProgressPaint;

    // 指针图片画笔
    private Paint mPointerBitmapPaint;

    //中间文本画笔
    private Paint mCenterTextPaint;

    // 绘制外层圆环的矩形
    private RectF mOuterArc;

    // 绘制内层圆环的矩形
    private RectF mInnerArc;

    // 绘制中间圆环的矩形
    private RectF mMiddleArc;

    // 中间进度圆环的值
    private float oval4;

    // 绘制中间进度圆环的矩形
    private RectF mMiddleProgressArc;

    // 圆环起始角度
    private static final float mStartAngle = 0f;

    // 圆环结束角度
    private static final float mEndAngle = 360f;

    // 指针全部进度
    private float mTotalAngle = 230f;

    // 指针当前进度
    private float mCurrentAngle = 0f;

    // View默认宽高值
    private int defaultSize;

    // 最小数字
    private int mMinNum = 0;

    // 最大数字
    private int mMaxNum = 950;

    //信用等级
    private String sesameLevel = "";

    //评估时间
    private String evaluationTime = "";

    private PaintFlagsDrawFilter mPaintFlagsDrawFilter;


    public NewRingView(Context context) {
        this(context, null);
    }

    public NewRingView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public NewRingView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        defaultSize = dp2px(300);
        //设置图片线条的抗锯齿
        mPaintFlagsDrawFilter = new PaintFlagsDrawFilter(0, Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG);

        //中间圆环画笔设置
        mMiddleRingPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mMiddleRingPaint.setStyle(Paint.Style.STROKE);
        mMiddleRingPaint.setStrokeCap(Paint.Cap.ROUND);
        mMiddleRingPaint.setStrokeWidth(5);
        mMiddleRingPaint.setColor(Color.RED);


        //中间圆环进度画笔设置
        mMiddleProgressPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mMiddleProgressPaint.setColor(GREEN_COLOR);
        mMiddleProgressPaint.setStrokeCap(Paint.Cap.ROUND);
        mMiddleProgressPaint.setStrokeWidth(5);
        mMiddleProgressPaint.setStyle(Paint.Style.STROKE);

        //指针图片画笔
        mPointerBitmapPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPointerBitmapPaint.setColor(GREEN_COLOR);

        //获取指针图片
        mBitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.zuobiao);
        mBitmapHeight = mBitmap.getHeight();
        mBitmapWidth = mBitmap.getWidth();
    }

    /**
     * dp2px
     */
    public int dp2px(int values) {

        float density = getResources().getDisplayMetrics().density;
        return (int) (values * density + 0.5f);
    }

    @SuppressLint("DrawAllocation")
    @Override
    protected void onDraw(Canvas canvas) {
        //设置画布绘图无锯齿
        canvas.setDrawFilter(mPaintFlagsDrawFilter);

        drawArc(canvas);
//    drawCalibration(canvas);
//    drawArcText(canvas);
//    drawCenterText(canvas);
        drawBitmapProgress(canvas);
    }

    private void drawBitmapProgress(Canvas canvas) {
        canvas.save();
        canvas.translate(radius, radius);
//        canvas.rotate(270);
        canvas.drawArc(mMiddleProgressArc, -mStartAngle, mCurrentAngle, false, mMiddleRingPaint);
//        canvas.rotate(68 + mCurrentAngle);
        Matrix matrix = new Matrix();
        matrix.preTranslate(-oval4 - mBitmapWidth * 3 / 8, -mBitmapHeight / 2);
        canvas.drawBitmap(mBitmap, matrix, mPointerBitmapPaint);
        canvas.restore();
    }

    private void drawArc(Canvas canvas) {
        canvas.save();
        canvas.translate(width / 2, height / 2);

        //画最外层的渐变圆环
//        canvas.rotate(140);
//        canvas.drawArc(mOuterArc, -mStartAngle, -mEndAngle, false, mGradientRingPaint);

        //绘制内圈圆形
        canvas.drawArc(mInnerArc, mStartAngle, mEndAngle, false, mMiddleRingPaint);
//        canvas.drawArc(mMiddleArc, -mStartAngle, -mEndAngle, false, mMiddleRingPaint);
        canvas.restore();
    }


    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        //确定View宽高
        width = w;
        height = h;

        //圆环半径
        radius = width / 2;

        //外层圆环
//        float oval1 = radius - mGradientRingPaint.getStrokeWidth() * 0.5f;
//        mOuterArc = new RectF(-oval1, -oval1, oval1, oval1);

        //中间和内层圆环
        float oval2 = radius * 5 / 8;
//        float oval3 = radius * 3 / 4;
        mInnerArc = new RectF(-oval2, -oval2, oval2, oval2);
//        mMiddleArc = new RectF(-oval3, -oval3, oval3, oval3);

        //中间进度圆环
        oval4 = radius * 6 / 8;
        mMiddleProgressArc = new RectF(-oval4, -oval4, oval4, oval4);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        setMeasuredDimension(resolveMeasure(widthMeasureSpec, defaultSize), resolveMeasure(heightMeasureSpec, defaultSize));
    }

    private int resolveMeasure(int measureSpec, int defaultSize) {
        int result = 0;
        int specSize = MeasureSpec.getSize(measureSpec);

        switch (MeasureSpec.getMode(measureSpec)) {

            case MeasureSpec.UNSPECIFIED:
                result = defaultSize;
                break;

            case MeasureSpec.AT_MOST:
                //设置warp_content时设置默认值
                result = Math.min(specSize, defaultSize);
                break;
            case MeasureSpec.EXACTLY:
                //设置math_parent 和设置了固定宽高值
                break;

            default:
                result = defaultSize;
        }

        return result;
    }
}
